

import UIKit

class ZhifuView: UIView { //WXApiDelegate
    var orderId:Int!
    var jifen:String!
    var money:String!
    var price:UILabel!
    var weixinBtn:UIButton!
    var zfbBtn:UIButton!
    var jifenBtn:UIButton!
    var wxCCImg:UIImageView!
    var zfbCCImg:UIImageView!
    var jifenCCImg:UIImageView!
    var payBtn:UIButton!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor=UIColor.black.withAlphaComponent(0.5)
        
        let boxview=UIView.init(frame: CGRect(x: 0, y: SCREEN_HEIGHT-420, width: SCREEN_WIDTH, height: 420))
        boxview.backgroundColor=UIColor.white
        self.addSubview(boxview)
        
        let close=UIButton.init(frame: CGRect(x: frame.size.width-50, y: 0, width: 50, height: 48))
        close.setImage(UIImage(named: "支付方式-关闭"), for: .normal)
        close.addTarget(self, action: #selector(closeView), for: .touchUpInside)
        boxview.addSubview(close)
        
        let title=UILabel.init(frame: CGRect(x: 0, y: 29, width: SCREEN_WIDTH, height: 15))
        title.text="选择支付方式"
        title.font=UIFont.systemFont(ofSize: 15)
        title.textAlignment = .center
        title.textColor=UIColor.init(hexString: "222222")
        boxview.addSubview(title)
        
        price=UILabel.init(frame: CGRect(x: 0, y: 64, width: SCREEN_WIDTH, height: 22))
        price.font=UIFont.boldSystemFont(ofSize: 24)
        price.textAlignment = .center
        price.textColor=UIColor.init(hexString: "222222")
        boxview.addSubview(price)
        
        //wx
        weixinBtn=UIButton.init(frame: CGRect(x: 15, y: 134, width: SCREEN_WIDTH-15, height: 47))
        boxview.addSubview(weixinBtn)
        weixinBtn.isSelected=true
        weixinBtn.addTarget(self, action: #selector(chooseWX), for: .touchUpInside)
        
        let wxImg=UIImageView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        wxImg.image=UIImage(named: "微信支付")
        weixinBtn.addSubview(wxImg)
        
        let wxTitle=UILabel.init(frame: CGRect(x: 41, y: 2, width: 50, height: 12))
        wxTitle.text="微信支付"
        wxTitle.font=UIFont.systemFont(ofSize: 12)
        wxTitle.textColor=UIColor.init(hexString: "333333")
        weixinBtn.addSubview(wxTitle)
        
        let wxLab=UILabel.init(frame: CGRect(x: 41, y: 19, width: 120, height: 9))
        wxLab.text="亿万用户的选择，更快更安全"
        wxLab.font=UIFont.systemFont(ofSize: 9)
        wxLab.textColor=UIColor.init(hexString: "8c8c8c")
        weixinBtn.addSubview(wxLab)
        
        let wximg2=UIImageView.init(frame: CGRect(x: 96, y: 2, width: 20, height: 12))
        wximg2.image=UIImage(named: "推荐")
        weixinBtn.addSubview(wximg2)
        
        wxCCImg=UIImageView.init(frame: CGRect(x: weixinBtn.frame.size.width-30, y: 8, width: 14, height: 14))
        wxCCImg.image=UIImage(named: "勾选-选中")
        wxCCImg.contentMode = .scaleAspectFit
        wxCCImg.isUserInteractionEnabled=true
        weixinBtn.addSubview(wxCCImg)
        
        let line1=UIView.init(frame: CGRect(x: 0, y: 46, width: SCREEN_WIDTH-30, height: 1))
        line1.backgroundColor=UIColor.init(hexString: "ebebeb")
        weixinBtn.addSubview(line1)
        
        
        //zfb
        zfbBtn=UIButton.init(frame: CGRect(x: 15, y: 198, width: SCREEN_WIDTH-15, height: 47))
        boxview.addSubview(zfbBtn)
        zfbBtn.addTarget(self, action: #selector(chooseZFB), for: .touchUpInside)

        let zfbImg=UIImageView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        zfbImg.image=UIImage(named: "支付宝")
        zfbBtn.addSubview(zfbImg)

        let zfbTitle=UILabel.init(frame: CGRect(x: 41, y: 9, width: 70, height: 12))
        zfbTitle.text="支付宝支付"
        zfbTitle.font=UIFont.systemFont(ofSize: 12)
        zfbTitle.textColor=UIColor.init(hexString: "333333")
        zfbBtn.addSubview(zfbTitle)

        zfbCCImg=UIImageView.init(frame: CGRect(x: zfbBtn.frame.size.width-30, y: 8, width: 14, height: 14))
        zfbCCImg.image=UIImage(named: "勾选")
        zfbCCImg.contentMode = .scaleAspectFit
        zfbCCImg.isUserInteractionEnabled=true
        zfbBtn.addSubview(zfbCCImg)

        let line2=UIView.init(frame: CGRect(x: 0, y: 46, width: SCREEN_WIDTH-30, height: 1))
        line2.backgroundColor=UIColor.init(hexString: "ebebeb")
        zfbBtn.addSubview(line2)
        
        //jifen
        jifenBtn=UIButton.init(frame: CGRect(x: 15, y: 198+17+47, width: SCREEN_WIDTH-15, height: 47))//x: 15, y: 262
        boxview.addSubview(jifenBtn)
        jifenBtn.addTarget(self, action: #selector(chooseJifen), for: .touchUpInside)
        
        let jifenImg=UIImageView.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        jifenImg.image=UIImage(named: "钱包余额")
        jifenBtn.addSubview(jifenImg)
        
        let jifenTitle=UILabel.init(frame: CGRect(x: 41, y: 9, width: 70, height: 12))
        jifenTitle.text="钱包余额"
        jifenTitle.font=UIFont.systemFont(ofSize: 12)
        jifenTitle.textColor=UIColor.init(hexString: "333333")
        jifenBtn.addSubview(jifenTitle)
        
        jifenCCImg=UIImageView.init(frame: CGRect(x: weixinBtn.frame.size.width-30, y: 8, width: 14, height: 14))
        jifenCCImg.image=UIImage(named: "勾选")
        jifenCCImg.contentMode = .scaleAspectFit
        jifenCCImg.isUserInteractionEnabled=true
        jifenBtn.addSubview(jifenCCImg)
        
        let line3=UIView.init(frame: CGRect(x: 0, y: 46, width: SCREEN_WIDTH-30, height: 1))
        line3.backgroundColor=UIColor.init(hexString: "ebebeb")
        jifenBtn.addSubview(line3)
        
        
        //pay
        payBtn=UIButton.init(frame: CGRect(x: 15, y: jifenBtn.frame.size.height+jifenBtn.frame.origin.y, width: SCREEN_WIDTH-30, height: 45))
        payBtn.layer.cornerRadius=5
        payBtn.backgroundColor=UIColor.init(hexString: "3e7dff")
        payBtn.addTarget(self, action: #selector(gopay), for: .touchUpInside)
        payBtn.titleLabel?.font=UIFont.systemFont(ofSize: 15)
        payBtn.setTitleColor(UIColor.white, for: .normal)
        payBtn.setTitle("去支付", for: .normal)
        boxview.addSubview(payBtn)
        
        //监听微信支付通知
        NotificationCenter.default.addObserver(self, selector: #selector(paySuccess), name: NSNotification.Name("WXPaySuccessNotification"), object: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func closeView(){
        self.removeFromSuperview()
    }
    
    @objc func  chooseZFB(){
    //    zfbBtn.isSelected=true
        weixinBtn.isSelected=false
        jifenBtn.isSelected=false
        wxCCImg.image=UIImage(named: "未勾选")
        jifenCCImg.image=UIImage(named: "未勾选")
        zfbCCImg.image=UIImage(named: "勾选-选中")
        payBtn.setTitle("支付宝支付", for: .normal)
        price.text=money
    }
    @objc func chooseWX(){
        zfbBtn.isSelected=false
        weixinBtn.isSelected=true
        jifenBtn.isSelected=false

        wxCCImg.image=UIImage(named: "勾选-选中")
        zfbCCImg.image=UIImage(named: "未勾选")
        jifenCCImg.image=UIImage(named: "未勾选")
        payBtn.setTitle("微信支付", for: .normal)
        price.text=money

    }
    @objc func chooseJifen(){
        zfbBtn.isSelected=false
        weixinBtn.isSelected=false
        jifenBtn.isSelected=true

        jifenCCImg.image=UIImage(named: "勾选-选中")
        zfbCCImg.image=UIImage(named: "未勾选")
        wxCCImg.image=UIImage(named: "未勾选")
        payBtn.setTitle("积分支付", for: .normal)
        price.text=jifen

    }
    @objc func gopay(){

//        let res=WXApiRequestHandler.jumpToBizPay()
//        if res != "" {
//            let alter=UIAlertView.init(title: "支付失败", message: res!, delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "")
//            alter.show()
//        }
        
      //  if zfbBtn.isSelected {
//        AlipaySDK.defaultService()?.payOrder(<#T##orderStr: String!##String?#>, fromScheme: alipaySchemes, callback: { (<#[AnyHashable : Any]?#>) in
//
//        })
      //  }
         if weixinBtn.isSelected {
            let dic=["id":orderId,"pay_method":"3"] as [String : Any]
         //   Network.request(.pay(dic)) { (JSON) in
               //调起微信支付
//                PayReq* req             = [[PayReq alloc] init];
//                req.partnerId           = [dict objectForKey:@"partnerid"];
//                req.prepayId            = [dict objectForKey:@"prepayid"];
//                req.nonceStr            = [dict objectForKey:@"noncestr"];
//                req.timeStamp           = stamp.intValue;
//                req.package             = [dict objectForKey:@"package"];
//                req.sign                = [dict objectForKey:@"sign"];
//                [WXApi sendReq:req completion:nil];
                
//                let req=PayReq.init()
//                req.partnerId  = JSON["data"]["data"]["partnerid"].string ?? ""
//                req.prepayId    = JSON["data"]["data"]["prepayid"].string ?? ""
//                req.nonceStr    = JSON["data"]["data"]["noncestr"].string ?? ""
//                req.timeStamp  = UInt32(Int32(JSON["data"]["data"]["timestamp"].string ?? "0") ?? 0)
//                req.package      = JSON["data"]["data"]["package"].string ?? ""
//                req.sign    = JSON["data"]["data"]["sign"].string ?? ""
//                WXApi.send(req, completion: nil)
              
//
//            } failure: { (err) in
//
//            }

        }else{  //积分兑换
            let dic=["id":orderId]
//            Network.request(.ffs_exchange(dic)) { (JSON) in
//                YProgressHUD.showToast(JSON["msg"].string)
//                let cvc=getCurrentVc()
//                cvc.navigationController?.popViewController(animated: true)
//            } failure: { (err) in
//
//            }
      }
        
        
    }
    
    @objc func paySuccess() {
        YProgressHUD.showToast("支付成功")
        self.removeFromSuperview()
        let cvc=getCurrentVc()
        cvc.navigationController?.popViewController(animated: true)
    }
    
  
    
}
